﻿class BabyToNow 
{
	private var inputYear:Number;
	private var inputMonth:Number;
	private var inputDate:Number;
	
	private var currentYear:Number;
	private var currentMonth:Number;
	private var currentDate:Number;	
	
	public function BabyToNow($year:Number,$month:Number,$date:Number)
	{
		inputYear=$year;
		inputMonth=$month;
		inputDate=$date;
		
		var cDate:Date=new Date();
		currentYear=cDate.getFullYear();
		currentMonth=cDate.getMonth()+1;
		currentDate = cDate.getDate();	
		var outPutDate:Array = countDate();
		
		//trace(outPutDate.toString());
	}
	public function countDate():Array
	{
		var dateArray:Array=[];
		var cDate:Number;
		var cMonth:Number;
		var cYear:Number;
		if (currentDate >= inputDate)
		{
			if (currentMonth >= inputMonth)
			{
				
				cDate = Number(currentDate)-Number(inputDate);
				cMonth = currentMonth - inputMonth;
				cYear= currentYear - inputYear;
			}else
			{
				cDate = currentDate-inputDate;
				cMonth = 12+currentMonth - inputMonth;
				cYear = currentYear - inputYear-1;
			}
		}else
		{
			if (currentMonth > inputMonth)
			{
				cDate = currentDate-inputDate+getDateOfMonth(currentMonth-1);
				cMonth = currentMonth - inputMonth-1;
				cYear = currentYear - inputYear;
			}else
			{
				if (currentMonth == 1)
				{
					cDate = currentDate-inputDate+31;
					cMonth = 12 - inputMonth;
					cYear = currentYear - inputYear-1;
				}else
				{
					cDate = currentDate-inputDate+getDateOfMonth(currentMonth-1);
					cMonth = currentMonth+12 - inputMonth;
					cYear = currentYear - inputYear-1;
				}
			}
		}
		dateArray[0] = cYear;
		dateArray[1] = cMonth;
		dateArray[2] = cDate;
		return dateArray;
	}
	private function getDateOfMonth($month:Number):Number
	{
		if ($month == 1||$month == 3||$month == 5||$month == 7||$month == 8||$month == 10||$month == 12)
		{
			return 31;
		}else if ($month == 4 || $month == 6 || $month == 9 || $month == 11)
		{
			return 30;
		}
		else
		{
			if((currentYear%4==0&&currentYear%100!=0)||(currentYear%400==0))
			{
				trace("闰年");
				return 29;
			}else{
				trace("非闰年");
				return 28;
			} 
		}
	}
	
}